/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.MRP.ProductionPlan;

import entity.MRP.ProductionPlan.PlannedDailyDemand;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import managedbean.MRP.ProductionPlan.GenerateMonthlyProductionPlanManagerBean.ColumnModel;
import session.stateless.MRP.ProductionPlan.DailyProductionPlanBeanLocal;

/**
 *
 * @author zhozho
 */
@ManagedBean(name="dailyProductionPlan")
@ViewScoped
public class GenerateDailyProductionPlanManagerBean implements Serializable {

    @EJB
    private DailyProductionPlanBeanLocal dailyProductionPlanBeanLocal;
    private String week;
    private List<ColumnModel> simpleColumns;
    private List<PlannedDailyDemand> database = new ArrayList<PlannedDailyDemand>();
    
    private List<DataObject> dataListA;
    private List<DataObject> dataListB;
    private List<DataObject> dataListC;
    private List<DataObject> dataListD;
    
    public GenerateDailyProductionPlanManagerBean() {
    }
    
    public void initView(PhaseEvent event){
        if (week == null)
        {
            database.clear();
            week = (String)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("week");            
        }
        if (week != null)
        {
            if (database.isEmpty())
            {
                //database = weeklyProductionPlanBeanLocal.getPlannedWeeklyDemands(month);
            }
            if (database.isEmpty())
            {            
                database = dailyProductionPlanBeanLocal.createPlannedDailyDemands(week);
            }        
        }
        createDynamicColumns();
    }
    
     private void createDynamicColumns() 
     {
        dataListA = new ArrayList<DataObject>();
        dataListB = new ArrayList<DataObject>();
        dataListC = new ArrayList<DataObject>();
        dataListD = new ArrayList<DataObject>();

        dataListA.add(gatherMonthlyDemand("A"));
        dataListA.add(gatherWorkingDaysInMonth());
        dataListA.add(gatherDailyDemand("A"));
        
        dataListB.add(gatherMonthlyDemand("B"));
        dataListB.add(gatherWorkingDaysInMonth());
        dataListB.add(gatherDailyDemand("B"));
        
        dataListC.add(gatherMonthlyDemand("C"));
        dataListC.add(gatherWorkingDaysInMonth());
        dataListC.add(gatherDailyDemand("C"));
        
        dataListD.add(gatherMonthlyDemand("D"));
        dataListD.add(gatherWorkingDaysInMonth());
        dataListD.add(gatherDailyDemand("D"));
        
        List<String> header = new ArrayList<String>();        
        
        for (int i = 0; i < database.size(); ++i)
        {
            header.add(database.get(i).getDayName());
        }
        
        simpleColumns = new ArrayList<ColumnModel>();
        simpleColumns.add(new ColumnModel("Demand Management", "type"));
        
        String[] dayMap = {"one", "two", "three", "four", "five"};
        for (int i = 0; i < database.size(); ++i) {
            simpleColumns.add(new ColumnModel(header.get(i), dayMap[i]));
        }
     }
     
     
     private DataObject gatherMonthlyDemand(String productName)
     {
         String[] list = new String[5];
         for (int i = 0; i < database.size(); ++i)
         {
             list[i] = database.get(0).getPlannedWeeklyDemand().getPlannedMonthlyDemand().getProductionPlan(productName).toString();
             if (list[i] == null)
             {
                 list[i] = "-";
             }
         }
         
         DataObject result = new DataObject("Monthly Demand", list[0], list[1], list[2], list[3], list[4]);
         return result;
     }
     
     private DataObject gatherWorkingDaysInMonth()
     {
        String[] list = new String[5];
        for (int i = 0; i < database.size(); ++i)
        {
            if (week != null)
            {
                list[i] = Long.toString(database.get(i).getPlannedWeeklyDemand().getPlannedMonthlyDemand().getNumberOfWorkingDays());
            }
        }
       
        DataObject result = new DataObject("Working Days in Month", list[0], list[1], list[2], list[3], list[4]);
        return result;
     }
     
     private DataObject gatherDailyDemand(String productName)
     {
         String[] list = new String[5];
         for (int i = 0; i < database.size(); ++i)
         {
             list[i] = database.get(0).getDailyDemand(productName).toString();
             if (list[i] == null)
             {
                 list[i] = "-";
             }
         }
         
         DataObject result = new DataObject("Daily Demand", list[0], list[1], list[2], list[3], list[4]);
         return result;
     }
     
    
     static public class DataObject implements Serializable{
        private String type;
        private String one;
        private String two;
        private String three;
        private String four;
        private String five;


        public DataObject(String type, String one, String two, String three, String four, String five) {

            this.type = type;
            this.one = one;
            this.two = two;
            this.three = three;
            this.four = four;
            this.five = five;
        }

        public String getFive() {
            return five;
        }

        public void setFive(String five) {
            this.five = five;
        }

        public String getFour() {
            return four;
        }

        public void setFour(String four) {
            this.four = four;
        }

        public String getOne() {
            return one;
        }

        public void setOne(String one) {
            this.one = one;
        }

        public String getThree() {
            return three;
        }

        public void setThree(String three) {
            this.three = three;
        }

        public String getTwo() {
            return two;
        }

        public void setTwo(String two) {
            this.two = two;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }               
    }

    public List<DataObject> getDataListA() {
        return dataListA;
    }

    public void setDataListA(List<DataObject> dataListA) {
        this.dataListA = dataListA;
    }

    public List<DataObject> getDataListB() {
        return dataListB;
    }

    public void setDataListB(List<DataObject> dataListB) {
        this.dataListB = dataListB;
    }

    public List<DataObject> getDataListC() {
        return dataListC;
    }

    public void setDataListC(List<DataObject> dataListC) {
        this.dataListC = dataListC;
    }

    public List<DataObject> getDataListD() {
        return dataListD;
    }

    public void setDataListD(List<DataObject> dataListD) {
        this.dataListD = dataListD;
    }

    public List<PlannedDailyDemand> getDatabase() {
        return database;
    }

    public void setDatabase(List<PlannedDailyDemand> database) {
        this.database = database;
    }

    public List<ColumnModel> getSimpleColumns() {
        return simpleColumns;
    }

    public void setSimpleColumns(List<ColumnModel> simpleColumns) {
        this.simpleColumns = simpleColumns;
    }

    public String getWeek() {
        return week;
    }

    public void setWeek(String week) {
        this.week = week;
    }
     
}
